package cn.dong.order.service.impl;

import cn.dong.api.utils.OrderNoUtils;
import cn.dong.base.dto.CourseDto;
import cn.dong.base.dto.MemberDto;
import cn.dong.base.dto.TradeOrderDto;
import cn.dong.order.domain.TradeOrder;
import cn.dong.order.feign.CourseService;
import cn.dong.order.feign.MemberService;
import cn.dong.order.mapper.TradeOrderMapper;
import cn.dong.order.service.TradeOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author You-Dong
 * @since 2021-02-23
 */
@Service
public class TradeOrderServiceImpl extends ServiceImpl<TradeOrderMapper, TradeOrder> implements TradeOrderService {

    @Autowired
    private CourseService courseService;

    @Autowired
    private MemberService memberService;

    /**
     * 创建订单
     *
     * @param courseId
     * @param memberId
     * @return
     */
    @Override
    public String createOrder(String courseId, String memberId) {

        // 判断该订单状态，用户已经生成了订单，无需在创建订单，课程不能重复购买
        QueryWrapper<TradeOrder> wrapper = new QueryWrapper<TradeOrder>();
        wrapper.eq("course_id", courseId);
        wrapper.eq("member_id", memberId);
        TradeOrder tradeOrder = super.baseMapper.selectOne(wrapper);
        // 将订单保存到数据库
        if (tradeOrder != null) {
            // 订单已经存在了，直接返回订单对象
            return tradeOrder.getId();
        }
        // 创建订单
        tradeOrder = new TradeOrder();

        // 课程
        CourseDto courseDto = courseService.getCourseDto(courseId);
        // 用户
        MemberDto memberDto = memberService.getMemberById(memberId);
        // 名称不一样，手动设置属性
        tradeOrder.setCourseCover(courseDto.getCover());
        tradeOrder.setCourseId(courseId);
        tradeOrder.setCourseTitle(courseDto.getTitle());
        tradeOrder.setMemberId(memberDto.getId());
        tradeOrder.setTeacherName(courseDto.getTeacherName());
        tradeOrder.setNickname(memberDto.getNickname());
        tradeOrder.setMobile(memberDto.getMobile());
        // 生成流水号
        String orderNo = OrderNoUtils.getOrderNo();
        tradeOrder.setOrderNo(orderNo);
        // 设置状态，未支付
        tradeOrder.setStatus(0);
        // 设置订单金额
        tradeOrder.setTotalFee(courseDto.getPrice().setScale(2));
        // 创建订单
        super.baseMapper.insert(tradeOrder);
        return tradeOrder.getId();
    }

    /**
     * 查询这个用户的订单
     *
     * @param id
     * @return
     */
    @Override
    public List<TradeOrderDto> getOrderByMemberId(String id) {
        // 结果
        List<TradeOrderDto> result = new ArrayList<>(20);
        // 查询
        QueryWrapper<TradeOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id", id);
        List<TradeOrder> list = super.baseMapper.selectList(queryWrapper);
        // 封装
        for (TradeOrder tradeOrder : list) {
            TradeOrderDto orderDto = new TradeOrderDto();
            BeanUtils.copyProperties(tradeOrder, orderDto);
            result.add(orderDto);
        }
        return result;
    }

    /**
     * 更改订单状态
     * @param order
     */
    @Override
    public void updateState(TradeOrder order) {
        // 修改状态
        order.setStatus(1);
        super.baseMapper.updateById(order);
    }

    /**
     * 查询订单状态
     *
     * @param orderNo
     * @return
     */
    @Override
    public boolean findStatus(String orderNo) {
        QueryWrapper<TradeOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderNo);
        TradeOrder order = super.baseMapper.selectOne(wrapper);
        if (order == null) {
            return false;
        }
        return order.getStatus() == 1;
    }


}
